package com.travelation;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.travelation.model.Post;

public class PostDetails extends Activity {

	public static final int MENU_MAIL = 1;
	public static final int MENU_BROWSE = 2;
	public static final int MENU_SHARE = 3;

	private TravelationApp app;
	private ProgressBar progressBar;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.postdetails);

		app = (TravelationApp) getApplication();

		progressBar = (ProgressBar) findViewById(R.id.postdetails_progress);
		progressBar.setIndeterminate(true);

		Post item = app.getCurrentItem();

		if (item != null) {
			ImageView icon = (ImageView) findViewById(R.id.postdetails_icon);
			new RetrieveImageTask(icon).execute(item.getPic175Url());

			TextView title = (TextView) findViewById(R.id.postdetails_title);
			title.setText(item.getTitle());

			CharSequence pricePrefix = getText(R.string.postdetails_price_prefix);
			TextView price = (TextView) findViewById(R.id.postdetails_price);
			price.setText(pricePrefix + item.getConvertedCurrentPrice().toString());

			TextView ownerId = (TextView) findViewById(R.id.postdetails_owner);
			ownerId.setText(item.getOwnerId());
			
			TextView rentedCount = (TextView) findViewById(R.id.postdetails_rented_count);
			rentedCount.setText(Integer.toString(item.getRentedCount()));

			TextView location = (TextView) findViewById(R.id.postdetails_location);
			location.setText(item.getAddress());
			
			TextView description = (TextView) findViewById(R.id.postdetails_description);
			description.setText(item.getDesc());

		} else {
			Toast.makeText(this,
					"Error, no current item selected, nothing to see here",
					Toast.LENGTH_LONG).show();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, PostDetails.MENU_MAIL, 0, R.string.postdetails_mail_menu);
		menu.add(0, PostDetails.MENU_BROWSE, 1,
				R.string.postdetails_browser_menu);
		menu.add(0, PostDetails.MENU_SHARE, 2, R.string.postdetails_share_menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case MENU_MAIL:
			shareDealUsingChooser("text/html");
			return true;
//		case MENU_BROWSE:
//			openDealInBrowser();
//			return true;
		case MENU_SHARE:
			shareDealUsingChooser("text/*"); // if we set something like
												// text/whateverhere then it's
												// gonna launch mail
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private void shareDealUsingChooser(String type) {
		Intent i = new Intent(Intent.ACTION_SEND);
		i.setType(type);
		i.putExtra(Intent.EXTRA_SUBJECT, "Subject:");
		i.putExtra(Intent.EXTRA_TEXT, createDealMessage());
		try {
			startActivity(Intent.createChooser(i, "Share deal ..."));
			// startActivity(i); // try this to see what happens when you don't
			// set a chooser
		} catch (android.content.ActivityNotFoundException ex) {
			Toast.makeText(
					PostDetails.this,
					"There are no chooser options installed for the " + type
							+ " + type.", Toast.LENGTH_SHORT).show();
		}
	}

//	private void openDealInBrowser() {
//		Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(app
//				.getCurrentItem().getDealUrl()));
//		startActivity(i);
//	}

	// TODO not i18n'd
	private String createDealMessage() {
		Post item = app.getCurrentItem();
		StringBuffer sb = new StringBuffer();
		sb.append("Check out this deal:\n");
		sb.append("\nTitle:" + item.getTitle());
		sb.append("\nPrice:" + item.getConvertedCurrentPrice());
		sb.append("\nLocation:" + item.getAddress());
//		sb.append("\nQuantity:" + item.getQuantity());
//		sb.append("\nURL:" + item.getDealUrl());
		return sb.toString();
	}

	private class RetrieveImageTask extends AsyncTask<String, Void, Bitmap> {
		private ImageView imageView;

		public RetrieveImageTask(ImageView imageView) {
			this.imageView = imageView;
		}

		@Override
		protected Bitmap doInBackground(String... args) {
			Bitmap bitmap = app.retrieveBitmap(args[0]);
			return bitmap;
		}

		@Override
		protected void onPostExecute(Bitmap bitmap) {
			progressBar.setVisibility(View.GONE);
			if (bitmap != null) {
				imageView.setImageBitmap(bitmap);
				imageView.setVisibility(View.VISIBLE);
			}
		}
	}
}
